package com.example.yuanboot.common.config.security;

import com.example.yuanboot.model.entity.system.SysUser;
import com.example.yuanboot.model.vo.jwt.JwtUser;
import com.example.yuanboot.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


/**
 * @author qingyan 2021-06-08
 */
@Component("userDetailsService")
@RequiredArgsConstructor
public class JwtUserDetailServiceImpl implements UserDetailsService {

    private final ISysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = sysUserService.getByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("登录用户不存在");
        }
        return new JwtUser(user.getId(), user.getUsername(), user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"));
    }

}
